//
//  YZWorkeNoteVM.m
//  VET
//
//  Created by 刘文靖 on 2018/7/10.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZWorkeNoteVM.h"
#import "YZWorkeNoteAPI.h"
#import "MJExtension.h"
#import "YZWorkNoteModel.h"
#import "YZWorkNoteLayout.h"
@interface YZWorkeNoteVM ()
@property (nonatomic, strong) NSMutableArray<YZWorkNoteModel *> *noteList;
@property (nonatomic, strong) NSMutableArray<YZWorkNoteLayout *> *layouts;
@property (nonatomic, assign) NSInteger currentPage;
@end
#define kPagePerCount 10
@implementation YZWorkeNoteVM
/**笔记列表*/
- (void)refreshNoteListWithCompletion:(void (^)(BOOL noMore))completion error:(void (^)(NSError *error))errorHandle{
    [self requestWorkNoteList:0 completion:completion error:errorHandle];
}

- (void)LoadMoreNoteListWithCompletion:(void (^)(BOOL noMore))completion error:(void (^)(NSError *error))errorHandle{
    [self requestWorkNoteList:self.currentPage completion:completion error:errorHandle];
}

- (void)requestWorkNoteList:(NSInteger)index completion:(void (^)(BOOL noMore))completion error:(void (^)(NSError *error))errorHandle{
    [[YZWorkeNoteAPI requestWorkNoteList:index] subscribeNext:^(NSDictionary *dict) {
        [self convertLayout:index dict:dict completion:completion];
        
    } error:^(NSError * _Nullable error) {
        errorHandle(error);
    }];
}

/**笔记列表(档案) */
- (void)refreshArchiveListWithType:(NSString *)type objId:(NSInteger)objId completion:(void (^)(BOOL noMore))completion error:(void (^)(NSError *error))errorHandle{
    
    [self requestArchivesWorkNoteList:type objId:objId index:0 completion:completion error:errorHandle];
}

- (void)LoadMoreArchivesListWithType:(NSString *)type objId:(NSInteger)objId completion:(void (^)(BOOL noMore))completion error:(void (^)(NSError *error))errorHandle{
    
     [self requestArchivesWorkNoteList:type objId:objId index:self.currentPage completion:completion error:errorHandle];
}

- (void)requestArchivesWorkNoteList:(NSString *)type objId:(NSInteger)objId index:(NSInteger)index  completion:(void (^)(BOOL noMore))completion error:(void (^)(NSError *error))errorHandle{
    [[YZWorkeNoteAPI requestArchivesWorkNoteList:type objId:objId index:index] subscribeNext:^(NSDictionary *dict) {
        [self convertLayout:index dict:dict completion:completion];
    }error:^(NSError * _Nullable error) {
        errorHandle(error);
    }];
}

- (void)convertLayout:(NSInteger)index dict:(NSDictionary *)dict completion:(void (^)(BOOL noMore))completion{
    if (index) {
        [self.noteList addObjectsFromArray:[YZWorkNoteModel mj_objectArrayWithKeyValuesArray:dict[@"list"]]];
        ++self.currentPage;
    }else{
        self.noteList = [YZWorkNoteModel mj_objectArrayWithKeyValuesArray:dict[@"list"]];
        self.currentPage = 1;
    }
    BOOL noMore = [dict[@"list"] count] < kPagePerCount;
    if (self.layouts) {
        [self.layouts removeAllObjects];
    }else{
        self.layouts = [NSMutableArray array];
    }
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (YZWorkNoteModel *note in self.noteList) {
            YZWorkNoteLayout *layout = [[YZWorkNoteLayout alloc] initWithNote:note];
            [self.layouts addObject:layout];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            completion(noMore);
        });
    });
}

/**添加笔记 */
- (RACSignal*)createNote:(NSDictionary *)dict{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[YZWorkeNoteAPI createNote:dict] subscribeNext:^(id  _Nullable x) {
            [subscriber sendNext:x];
            [subscriber sendCompleted];
            
        } error:^(NSError * _Nullable error) {
            [subscriber sendError:error];
        } completed:^{
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];
}
/**更新笔记 */
- (RACSignal*)updateNote:(NSDictionary *)dict{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[YZWorkeNoteAPI updateNote:dict] subscribeNext:^(id  _Nullable x) {
            [subscriber sendNext:x];
            [subscriber sendCompleted];
            
        } error:^(NSError * _Nullable error) {
            [subscriber sendError:error];
        } completed:^{
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];
}
/**删除笔记 */
- (RACSignal*)deleteNote:(NSInteger )noteId{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[YZWorkeNoteAPI deleteNote:noteId] subscribeNext:^(id  _Nullable x) {
            [subscriber sendCompleted];
            
        } error:^(NSError * _Nullable error) {
            [subscriber sendError:error];
            [subscriber sendCompleted];
        } completed:^{
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];
}


@end
